#include "Mapview.h"
#include <string>;
#include <qpainter.h>
#include <qimage.h>
#include <qicon.h>
using namespace std;

Mapview::Mapview(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
    edgeNum = 0;
    route = NULL;
    mousePosition = "";
    this->resize(1000, 800);
    this->setWindowIcon(QIcon(":Icon/image/BeijingSubwaylogo.png"));
    this->setWindowTitle("总线视图");
}

void Mapview::paintEvent(QPaintEvent*)
{
    QPainter p(this);  //创建绘图对象
    QPixmap im(":/Icon/image/map2.png");
    QRect r(0, 0, this->width(), this->height());
    p.drawPixmap(r, im);
    Graph g;

    QRect r2(200, 10, 200, 50);
    p.drawText(r2, QString::fromStdString(mousePosition));  //显示鼠标点击位置坐标

    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    int diameter = 10;  //圆点的直径
    int R = 0;   
    int G = 0;
    int B = 0; //线路颜色的RGB值

    for (int i = 0; i < edgeNum ; i++) {   //站间距不等长，必须一次画一条边
        QFile location(":/files/files/location.txt");
        location.open(QIODevice::ReadOnly);
        QTextStream in(&location);
        while (true)
        {
            QString position = in.readLine();
            position = position.trimmed();
            if (position.isNull()) {
                break;
            }
            QStringList parts = position.split(",");
            if (g.getStationName(route[i].to) == "复兴门") {
                qDebug() << parts.at(0);
            }
            if (g.getStationName(route[i].from) == parts.at(0).toStdString()) {
                x1 = parts.at(1).toInt();
                y1 = parts.at(2).toInt();
            }
            else if (g.getStationName(route[i].to) == parts.at(0).toStdString()) {
                x2 = parts.at(1).toInt();
                y2 = parts.at(2).toInt();
            }
        }
        location.close();

        /*获取线路颜色*/
        QFile colorL(":/files/files/colorL.txt");
        colorL.open(QIODevice::ReadOnly);
        QTextStream in2(&colorL);
        while (true) {
            QString array = in2.readLine();
            array = array.trimmed();
            if (array.isNull()) {
                break;
            }
            QStringList parts = array.split(",");   //按","分割
            if (parts.at(0).toInt() == route[i].line) {
                R = parts.at(1).toInt();
                G = parts.at(2).toInt();
                B = parts.at(3).toInt();
                break;
            }
        }

        /*画点画线*/
        p.setPen(QPen(QColor(R, G, B), 5, Qt::SolidLine, Qt::RoundCap));
        p.drawLine(x1 + diameter / 2, y1+ diameter / 2, x2 + diameter / 2, y2 + diameter / 2);
        p.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap));
        if (i == 0) {    //第一条边，出发站设为红色，下一站为蓝色
            p.setBrush(QBrush(QColor(255, 48, 48), Qt::SolidPattern));
            p.drawEllipse(x1, y1, diameter, diameter);
            p.setBrush(QBrush(QColor(30, 144, 255), Qt::SolidPattern));
            p.drawEllipse(x2, y2, diameter, diameter);
        }
        else if(i== edgeNum - 1){   //最后一条边，终点站为绿色，上一站是蓝色
            p.setBrush(QBrush(QColor(30, 144, 255), Qt::SolidPattern));
            p.drawEllipse(x1, y1, diameter, diameter);
            p.setBrush(QBrush(Qt::green, Qt::SolidPattern));
            p.drawEllipse(x2, y2, diameter, diameter);
        }
        else if (route[i].line != route[i - 1].line) {  //换乘站设为黄色
            p.setBrush(QBrush(QColor(255, 215, 0), Qt::SolidPattern));
            p.drawEllipse(x1, y1, diameter, diameter);
            p.setBrush(QBrush(QColor(30, 144, 255), Qt::SolidPattern));
            p.drawEllipse(x2, y2, diameter, diameter);
        }
        else {
            p.setBrush(QBrush(QColor(30, 144, 255), Qt::SolidPattern));
            p.drawEllipse(x1, y1, diameter, diameter);
            p.drawEllipse(x2, y2, diameter, diameter);
        }
    }
    p.end();
}

void Mapview::setRoute(Edge* edge,int num) {
    delete[] route;
    edgeNum = num;
    Edge* s = new Edge[num];
    for (int i = 0; i < num; i++) {
        s[i] = edge[i];
    }
    route = s;
    update();
}

void Mapview::mousePressEvent(QMouseEvent* event) {
    QPoint p2 = event->pos();
    QString str2 = QString::number(p2.x()) + " , " + QString::number(p2.y());
    mousePosition = str2.toStdString();
    update();
}

void Mapview::clearRoute() {
    delete[] route;
    edgeNum = 0;
    route = NULL;
    update();
}

Mapview::~Mapview()
{
    delete[] route;
}
